/*
 * WebComponent.java
 *
 * Created on March 26, 2007, 6:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.auth.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import org.atomojo.auth.service.app.AuthApplication;
import org.atomojo.auth.service.db.AuthDB;
import org.restlet.Component;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.routing.VirtualHost;

/**
 *
 * @author alex
 */
public class WebComponent extends Component {
   
   public static String LOG_NAME = "org.atomojo.www.auth";
   AuthDB db;
   Configuration config;
   
   /** Creates a new instance of WebComponent */
   public WebComponent(Configuration config,AuthDB db) {
      this.config = config;
      this.db = db;
      getLogService().setLoggerName(LOG_NAME);
      
      if (config.getKeyStorePath()!=null) {
         getContext().getParameters().add("keystorePath",config.getKeyStorePath().getAbsolutePath());
         getContext().getParameters().add("keystorePassword",config.getKeyStorePassword());
         getContext().getParameters().add("keyPassword",config.getKeyStorePassword());
      }
      
      /*
      VirtualHost lhost = new VirtualHost(getContext());
      lhost.setHostDomain("localhost");
      configure(lhost,null);
      getHosts().add(lhost);
       */
      
      for (Configuration.Interface iface : config.getInterfaces()) {
         if (iface.isSecure()) {
            getContext().getLogger().info("https listening on "+iface.getAddress()+":"+iface.getPort());
            Server server = getServers().add(Protocol.HTTPS, iface.getAddress().equals("*") ? null : iface.getAddress(), iface.getPort());
            server.getContext().getParameters().add("keystorePath",config.getKeyStorePath().getAbsolutePath());
            server.getContext().getParameters().add("keystorePassword",config.getKeyStorePassword());
            server.getContext().getParameters().add("keyPassword",config.getKeyStorePassword());
         } else {
            getContext().getLogger().info("http listening on "+iface.getAddress()+":"+iface.getPort());
            getServers().add(Protocol.HTTP, iface.getAddress().equals("*") ? null : iface.getAddress(), iface.getPort());            
         }
         
         for (String name : iface.getHosts().keySet()) {
            final Configuration.Host host = iface.getHosts().get(name);
            VirtualHost vhost = new VirtualHost(getContext());
            getContext().getLogger().info("Adding host "+host.getName());
            if ("*".equals(iface.getAddress())) {
               vhost.setServerAddress(".*");
            } else {
               try {
                  InetAddress addr = InetAddress.getByName(iface.getAddress());
                  String saddr = addr.toString();
                  saddr = saddr.substring(saddr.indexOf('/')+1);
                  getLogger().info("Restricting "+host.getName()+" to address "+saddr);
                  vhost.setServerAddress(saddr);
               } catch (UnknownHostException ex) {
                  getLogger().severe("Cannot resolve host name "+iface.getAddress());
               }
            }
            if (host.getPort()==null) {
               vhost.setHostPort(Integer.toString(iface.getPort()));
            } else {
               vhost.setHostPort(host.getPort());
            }
            if (!"*".equals(name)) {
               if (name!=null) {
                  vhost.setHostDomain(name);
               }
            }
            configure(vhost,host);

            getHosts().add(vhost);
            
         }
      }
      
      // Add the clients
      getClients().add(Protocol.FILE);
      getClients().add(Protocol.HTTP);
      getClients().add(Protocol.HTTPS);

      for (Server server : getServers()) {
         try {
            server.start();
         } catch (Exception ex) {
            getLogger().log(Level.SEVERE,"Cannot start server.",ex);
         }
      }
      
   }
   
   protected void configure(VirtualHost vhost,Configuration.Host host)
   {
      vhost.attachDefault(new AuthApplication(vhost.getContext(),db));
   }
   
}
